<?php
class CString{
	function __construct() {
	
	}
	
	static function subStrs($content, $length, $add = 'Y', $theEnd = '...') {
		if ($length && strlen ( $content ) > $length) {
			if (strtolower ( CHAR_SET ) != 'utf-8') {
				$retstr = '';
				for($i = 0; $i < $length - 2; $i ++) {
					$retstr .= ord ( $content [$i] ) > 127 ? $content [$i] . $content [++ $i] : $content [$i];
				}
				$add == 'Y' && $retstr .= " $theEnd";
				return $retstr;
			} else {
				for($i = 0; $i < $length; $i ++) {
					$temp_str = substr ( $content, 0, 1 );
					if (ord ( $temp_str ) > 127) {
						$i ++;
						if ($i < $length) {
							$new_str [] = substr ( $content, 0, 3 );
							$content = substr ( $content, 3 );
						}
					} else {
						$new_str [] = substr ( $content, 0, 1 );
						$content = substr ( $content, 1 );
					}
				}
				if ($add == 'Y') {
					return join ( $new_str ) . $theEnd;
				} else {
					return join ( $new_str );
				}
			}
		}
		return $content;
	}
	/*
	* 中文截取，支持gb2312,gbk,utf-8,big5
	*
	* @param string $str 要截取的字串
	* @param int $start 截取起始位置
	* @param int $length 截取长度
	* @param string $charset utf-8|gb2312|gbk|big5 编码
	* @param $suffix 是否加尾缀
	*/
	static public function csubstr($str, $start = 0, $length, $charset = "utf-8", $endstr="...") {

		
		if (function_exists ( "mb_substr" )){
			
			if($endstr!=''&&mb_strlen($str,$charset)>$length){
				
				return mb_substr ( $str, $start, $length, $charset ).$endstr;
				
			}else{
				
				return mb_substr ( $str, $start, $length, $charset );
				
			}
			
		}
		$re ['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
		$re ['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
		$re ['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
		$re ['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
		preg_match_all ( $re [$charset], $str, $match );
		$slice = join ( "", array_slice ( $match [0], $start, $length ) );
		if ($endstr!='')
			return $slice . $endstr;
		return $slice;
	}
	
	static public function randstr($lenth) {
		mt_srand ( ( double ) microtime () * 1000000 );
		for($i = 0; $i < $lenth; $i ++) {
			$randval .= mt_rand ( 0, 9 );
		}
		$randval = substr ( md5 ( $randval ), mt_rand ( 0, 32 - $lenth ), $lenth );
		return $randval;
	}
	
	static public function randYzStr($len = 6, $format = 'ALL') {
		switch ($format) {
			case 'ALL' :
				$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
				break;
			
			case 'CHAR' :
				$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
				break;
			
			case 'NUMBER' :
				$chars = '0123456789';
				break;
			
			default :
				$chars = 'ABCDEFGHIJKLMNPQRSTUVWXYZ123456789';
				break;
		}
		
		$string = '';
		
		while ( strlen ( $string ) < $len ) {
			$string .= substr ( $chars, (mt_rand () % strlen ( $chars )), 1 );
		}
		
		return $string;
	}
	
	static function num_rand($lenth) {
		mt_srand ( ( double ) microtime () * 1000000 );
		for($i = 0; $i < $lenth; $i ++) {
			$randval .= mt_rand ( 0, 9 );
		}
		return $randval;
	}
	
	static function checkEmail($email) {
		return strlen ( $email ) > 6 && preg_match ( "/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/", $email );
	}
	
	static function checkUserName($name) {
		return preg_match ( "/^[0-9a-zA-Z\-\xa1-\xff]{4,20}$/", $name );
	}
	
	static function checkUserPass($pass) {
		return preg_match ( "/^[0-9a-zA-Z\-\xa1-\xff]{4,}$/", $pass );
	}
	
	static function checkNum($num) {
		return preg_match ( "/^\d+$/", $num );
	}
	
	static function checkLetter($String) {
		return preg_match ( "/^[a-zA-Z]+$/", $String );
	}
	
	static function checkSpecialWord($String) {
		return preg_match ( "/^[0-9a-zA-Z\-\xa1-\xff]+$/", $String );
	}
	
	static function checkIsFilled($thisArray, $thisPost) {
		if (count ( $thisPost ) < 1) {
			return false;
		}
		
		foreach ( $thisArray as $item ) {
			if ($thisPost [$item] == "" || ! isset ( $thisPost [$item] )) {
				return false;
			}
		}
		
		return true;
	}
	
	static function checkValueLength($valueArray, $lengthArray) {
		$num = count ( $valueArray );
		for($i = 0; $i < $num; $i ++) {
			if (strlen ( $valueArray [$i] ) > $lengthArray [$i]) {
				return $i;
			}
		}
		return - 1;
	}
	
	static public function getIp(){
		
		if(!empty($_SERVER["HTTP_CLIENT_IP"])) $cip = $_SERVER["HTTP_CLIENT_IP"];
		
		else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) {
		
			$cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
		
		}
		
		else if(!empty($_SERVER["REMOTE_ADDR"])) {
			
			$cip = $_SERVER["REMOTE_ADDR"];
			
		}
		
		else $cip = "无法获取！";
		
		return $cip;	
	}
	
	public static function maskUid($uid) {
        if (trim($uid)=='') {
            return "";
        }
        $pos = strpos($uid,'@');
        if ($pos < 0) {
            return "****";
        } else if ($pos <= 4) {
            return "****" . substr($uid,$pos);
        } else {
            return substr($uid,0,4)."****" . substr($uid,$pos);
        }
    }

}
